package com.hhit.bigscreenbackmanage.service.impl.jjgl;



import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.entity.jjgl.TJjglCspsyb;
import com.hhit.bigscreenbackmanage.dao.jjgl.TJjglCspsybDao;
import com.hhit.bigscreenbackmanage.service.jjgl.TJjglCspsybService;
import com.hhit.bigscreenbackmanage.util.PageUtil;
import com.hhit.bigscreenbackmanage.vo.jjgl.*;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 初设评审月报(TJjglCspsyb)表服务实现类
 *
 * @author makejava
 * @since 2021-07-20 21:51:57
 */
@Service("tJjglCspsybService")
public class TJjglCspsybServiceImpl implements TJjglCspsybService {
    @Resource
    private TJjglCspsybDao tJjglCspsybDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TJjglCspsyb queryById(String id) {
        return this.tJjglCspsybDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param jsonObject
     * @return 对象列表
     */
    @Override
    public Map<String,Object> queryAllByLimit(JSONObject jsonObject) {
        Map<String,Object> out = new HashMap<>();
        PageUtil page = new PageUtil();
        String pageSize = jsonObject.get("pageSize").toString();
        String pageNo = jsonObject.get("pageNo").toString();
        int count = tJjglCspsybDao.queryAllCount(jsonObject);
        page.pageCount=Integer.parseInt(pageSize);// 每页数
        page.init(count,Integer.parseInt(pageNo));//参数1：总行数，参数2：当前页
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<TJjglCspsyb> tJjglCspsybs = tJjglCspsybDao.queryAll(jsonObject);
        out.put("page",page);
        out.put("list",tJjglCspsybs);
        return out;
    }

    /**
     * 新增数据
     *
     * @param tJjglCspsyb 实例对象
     * @return 实例对象
     */
    @Override
    public TJjglCspsyb insert(TJjglCspsyb tJjglCspsyb) {
        this.tJjglCspsybDao.insert(tJjglCspsyb);
        return tJjglCspsyb;
    }

    /**
     * 修改数据
     *
     * @param tJjglCspsyb 实例对象
     * @return 实例对象
     */
    @Override
    public TJjglCspsyb update(TJjglCspsyb tJjglCspsyb) {
        this.tJjglCspsybDao.update(tJjglCspsyb);
        return this.queryById(tJjglCspsyb.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(List<String> id) {
        return this.tJjglCspsybDao.deleteById(id) > 0;
    }

    @Override
    public Map<String, Object> getSsxx(SsXxVO obj) {
        Map<String,Object> out = new HashMap<>();
        int[] allDy = {500, 220 , 110, 35};
        List<String> dyListBean = new ArrayList<>();
        String[] gs = {"0", "0","0", "0"};
        String[] jsje = {"0", "0","0", "0"};
        String[] juesje = {"0", "0","0", "0"};
        String[] dyList = {"35", "110", "220", "500"};
        for (int i = 0; i <dyList.length ; i++) {
            for (int dy : allDy) {
                if(Integer.parseInt(dyList[i]) > dy){
                    dyListBean.add(String.valueOf(dy));
                }
            }
            SsXxVO ssxx = tJjglCspsybDao.getSsxx(dyList[i], dyListBean);
            gs[i] = ssxx.getGs();
            jsje[i] = ssxx.getJsje();
            juesje[i] = ssxx.getJuesje();
        }
        out.put("dyList",dyList);
        List<Object> result = new ArrayList<>();
        out.put("gs",gs);
        out.put("jsje",jsje);
        out.put("juesje",juesje);
        return out;
    }

    @Override
    public Map<String, Object> getJjglSsxx(SsXxVO obj) {
        Map<String,Object> out = new HashMap<>();
        List<Map> mapList = new ArrayList<>();
        int[] allDy = {500, 220 , 110, 35};
        List<String> dyListBean = new ArrayList<>();
        String[] dyList = {"35", "110", "220", "500"};
        for (int i = 0; i <dyList.length ; i++) {
            for (int dy : allDy) {
                if(Integer.parseInt(dyList[i]) > dy){
                    dyListBean.add(String.valueOf(dy));
                }
            }
            SsXxVO ssxx = tJjglCspsybDao.getSsxx(dyList[i], dyListBean);
            Map<String,Object> map = new HashMap<>();
            map.put("name",dyList[i]+"kV");
            map.put("gaisuan",ssxx.getGs());
            map.put("jiesuan",ssxx.getJsje());
            map.put("juesuan",ssxx.getJuesje());
            mapList.add(map);
        }
        out.put("ssBar",mapList);
        return out;
    }

    @Override
    public Map<String, Object> getGsGsPc(JSONObject obj) {
        Map<String,Object> out = new HashMap<>();
        out.put("cwb",tJjglCspsybDao.getGsGsPc_cwb());
        out.put("cshi",tJjglCspsybDao.getGsGsPc_cshi());
        out.put("pcxm",tJjglCspsybDao.getGsGsPc_pcxm());
        return out;
    }

    @Override
    public Map<String, Object> getNMggz(JSONObject obj) {
        Map<String,Object> out = new HashMap<>();
        String monthJe = tJjglCspsybDao.nmggzMonth();
        String yearJe = tJjglCspsybDao.nmggzYear();
        out.put("monthJe",monthJe);
        out.put("yearJe",yearJe);
        return out;
    }

    @Override
    public Map<String, Object> getXyGlgz(JSONObject obj) {
        Map<String,Object> out = new HashMap<>();
        List<XyglgzVO> xyGlgz = tJjglCspsybDao.getXyGlgz(obj);
        if(!CollectionUtils.isEmpty(xyGlgz)){
            Map<String, List<XyglgzVO>> titleCollect = xyGlgz.stream().collect(Collectors.groupingBy(XyglgzVO::getTitle));
            out.put("result",titleCollect);
        }else {
            out.put("result",null);
        }
        return out;
    }

    @Override
    public void addXyGlgz(XyglgzAddVO list) {
        List<XyglgzVO> addList = list.getList();
        for (int i = 0; i < addList.size(); i++) {
            addList.get(i).setId(IdUtil.fastSimpleUUID());
            addList.get(i).setSort(i);
        }
        tJjglCspsybDao.addXyGlgz(addList,list.getDataTime());
    }

    @Override
    public Map<String, Object> getGmJyxCsPsYjCj(JSONObject obj) {
        Map<String,Object> out = new HashMap<>();
        String[] jh = {"0", "0" , "0"};
        String[] sj = {"0", "0" , "0"};
        String[] dydj = {"35", "110" , "220"};
        for (int i = 0; i < dydj.length; i++) {
            GmJyxCsPsYjCjVO gmJyxCsPsYjCj = tJjglCspsybDao.getGmJyxCsPsYjCj(dydj[i]);
            jh[i] = String.valueOf((Integer.parseInt(gmJyxCsPsYjCj.getJh())+1));
            sj[i] = gmJyxCsPsYjCj.getJh();
        }
        out.put("jh",jh);
        out.put("sj",sj);
        out.put("dydj",dydj);
        return out;
    }

    @Override
    public Map<String, Object> getCsPsJhZx(JSONObject obj) {
        Map<String,Object> out = new HashMap<>();
        String[] jh = {"0", "0" , "0", "0"};
        String[] sj = {"0", "0" , "0", "0"};
        String[] dydj = {"35", "110" , "220" , "500"};
        for (int i = 0; i < dydj.length; i++) {
            GmJyxCsPsYjCjVO csPsJhZx = tJjglCspsybDao.getCsPsJhZx(dydj[i]);
            jh[i] = csPsJhZx.getJh();
            sj[i] = csPsJhZx.getSj();
        }
        out.put("jh",jh);
        out.put("sj",sj);
        out.put("dydj",dydj);
        return out;
    }

    @Override
    public Map<String, Object> getYysZxDpList(JSONObject jsonObject) {
        if(StringUtils.isEmpty(jsonObject.get("dataFlag")) || StringUtils.isEmpty(jsonObject.get("dydj"))){
            throw new RuntimeException("必要参数 不能为空");
        }
        String dataFlag = jsonObject.get("dataFlag").toString();
        Map<String,Object> out = new HashMap<>();
        PageUtil page = new PageUtil();
        String pageSize = jsonObject.get("pageSize").toString();
        String pageNo = jsonObject.get("pageNo").toString();
        page.pageCount=Integer.parseInt(pageSize);// 每页数
        // 计划查询 年度计划表
        if("jh".equals(dataFlag)){
            int count = tJjglCspsybDao.getYysZxDpList_jhCount(jsonObject);
            page.init(count,Integer.parseInt(pageNo));//参数1：总行数，参数2：当前页
            jsonObject.put("rowNum",page.getRowNum());
            jsonObject.put("pageCount",page.getPageCount());
            List<YysZxDpListVO> yysZxDpList = tJjglCspsybDao.getYysZxDpList_jh(jsonObject);
            out.put("page",page);
            out.put("list",yysZxDpList);
        }
        // 实际查询 初设月报
        if("sj".equals(dataFlag)){
            int count = tJjglCspsybDao.getYysZxDpList_sjCount(jsonObject);
            page.init(count,Integer.parseInt(pageNo));//参数1：总行数，参数2：当前页
            jsonObject.put("rowNum",page.getRowNum());
            jsonObject.put("pageCount",page.getPageCount());
            List<YysZxDpListVO> yysZxDpList = tJjglCspsybDao.getYysZxDpList_sj(jsonObject);
            out.put("page",page);
            out.put("list",yysZxDpList);
        }
        return out;
    }

    @Override
    public Map<String, Object> getGsGsPsDpList(JSONObject jsonObject) {
        if(StringUtils.isEmpty(jsonObject.get("dataFlag"))){
            throw new RuntimeException("必要参数 不能为空");
        }
        Map<String,Object> out = new HashMap<>();
        PageUtil page = new PageUtil();
        String pageSize = jsonObject.get("pageSize").toString();
        String pageNo = jsonObject.get("pageNo").toString();
        page.pageCount=Integer.parseInt(pageSize);// 每页数
        int count = tJjglCspsybDao.getGsGsPsDpListCount(jsonObject);
        page.init(count,Integer.parseInt(pageNo));//参数1：总行数，参数2：当前页
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<GsGsPsDpListVO> gsGsPsDpList = tJjglCspsybDao.getGsGsPsDpList(jsonObject);
        out.put("page",page);
        out.put("list",gsGsPsDpList);
        return out;
    }

    @Override
    public Map<String, Object> getNmgGzDpList(JSONObject jsonObject) {
        Map<String,Object> out = new HashMap<>();
        PageUtil page = new PageUtil();
        String pageSize = jsonObject.get("pageSize").toString();
        String pageNo = jsonObject.get("pageNo").toString();
        page.pageCount=Integer.parseInt(pageSize);// 每页数
        int count = tJjglCspsybDao.getNmgGzDpListCount(jsonObject);
        page.init(count,Integer.parseInt(pageNo));//参数1：总行数，参数2：当前页
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<NmgGzDpListVO> gsGsPsDpList = tJjglCspsybDao.getNmgGzDpList(jsonObject);
        out.put("page",page);
        out.put("list",gsGsPsDpList);
        return out;
    }

    @Override
    public Map<String, Object> getGmJyxCsPsYjCjDpList(JSONObject jsonObject) {
        Map<String,Object> out = new HashMap<>();
        PageUtil page = new PageUtil();
        String pageSize = jsonObject.get("pageSize").toString();
        String pageNo = jsonObject.get("pageNo").toString();
        page.pageCount=Integer.parseInt(pageSize);// 每页数
        int count = tJjglCspsybDao.getGmJyxCsPsYjCjDpListCount(jsonObject);
        page.init(count,Integer.parseInt(pageNo));//参数1：总行数，参数2：当前页
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        List<YysZxDpListVO> list = tJjglCspsybDao.getGmJyxCsPsYjCjDpList(jsonObject);
        out.put("page",page);
        out.put("list",list);
        return out;
    }

    /**
     *
     * @param a
     * @return
     */
    public static String twoNum(String a) {
        float af = Float.parseFloat(a);
        //“0.0000”确定精度
        DecimalFormat dF = new DecimalFormat("0.00");
        return dF.format(af);
    }
}